﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using hospital_registration_system.Entity;
using hospital_registration_system.Utility;

namespace hospital_registration_system.manager
{
    public partial class management : System.Web.UI.Page
    {
        /// <summary>
        /// 绑定科室管理GridView数据源
        /// </summary>
        private void gdvDepartmentsBind()
        {
            List<Departments> depart = ManagerDepartments.getAllDepartments();
            if (depart != null)
            {
                gdvDepartments.DataSource = depart;//为GridView控件指名数据源
                gdvDepartments.DataBind();//GridView控件绑定数据
            }
            else
            {
                Response.Write("<script language='javascript'>alert('发生错误！科室未查询到数据');</script>");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                gdvDepartmentsBind();//绑定科室数据源
                gdvDoctorBind();//绑定医生数据源
                gdvIllnessBind();//绑定疾病数据源
            }
            
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdvDepartments.PageIndex = e.NewPageIndex;
            gdvDepartmentsBind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gdvDepartments.EditIndex = -1;
            gdvDepartmentsBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //执行循环，保证每条数据都可以更新
            for (int i = 0; i <= gdvDepartments.Rows.Count; i++)
            {
                //首先判断是否是数据行
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //当鼠标停留时更改背景色
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#d3dfc0'");
                    //当鼠标移开时还原背景色
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string departID = gdvDepartments.DataKeys[e.RowIndex].Values["id"].ToString(); //取出要删除记录的主键值
            string msg = ManagerDepartments.deleteDepartment(Convert.ToInt32(departID));//执行删除并获取返回信息
            Response.Write("<script language=javascript>alert('" + msg + "');</script>");//弹出提示
            gdvDepartments.EditIndex = -1;
            gdvDepartmentsBind();//重新绑定数据
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gdvDepartments.EditIndex = e.NewEditIndex;  //GridView编辑项索引等于单击行的索引
            gdvDepartmentsBind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //封装修改对象
            Departments departments = new Departments(
                int.Parse(gdvDepartments.DataKeys[e.RowIndex].Values["id"].ToString()),
                ((TextBox)gdvDepartments.Rows[e.RowIndex].FindControl("TextBox2")).Text.ToString(),
                int.Parse(((TextBox)(gdvDepartments.Rows[e.RowIndex].FindControl("TextBox3"))).Text.ToString()),
                int.Parse(((TextBox)(gdvDepartments.Rows[e.RowIndex].FindControl("TextBox4"))).Text.ToString()),
                ((TextBox)(gdvDepartments.Rows[e.RowIndex].FindControl("TextBox5"))).Text.ToString(),
                DateTime.Parse(((TextBox)(gdvDepartments.Rows[e.RowIndex].FindControl("TextBox6"))).Text.ToString()),
                ((TextBox)(gdvDepartments.Rows[e.RowIndex].FindControl("TextBox7"))).Text.ToString(),
                DateTime.Parse(((TextBox)(gdvDepartments.Rows[e.RowIndex].FindControl("TextBox8"))).Text.ToString()),
                int.Parse(((TextBox)(gdvDepartments.Rows[e.RowIndex].FindControl("TextBox9"))).Text.ToString())
                );
            string msg = ManagerDepartments.updateDepartment(departments);//执行修改
            gdvDepartments.EditIndex = -1;
            gdvDepartmentsBind();//刷新显示
            Response.Write("<script language=javascript>alert('" + msg + "');</script>");//返回提示
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string msg = ManagerDepartments.insertDepartment(tbAdd.Text);//执行插入
            gdvDepartments.EditIndex = -1;
            gdvDepartmentsBind();
            Response.Write("<script language=javascript>alert('" + msg + "');</script>");
        }


//以下为医生管理的代码--------------------------------------------------------------------------------------

        /// <summary>
        /// 绑定医生管理GridView数据源
        /// </summary>
        private void gdvDoctorBind()
        {
            List<Doctor> doctors = ManagerDoctor.getAllDoctor();
            if (doctors != null)
            {
                gdvDoctor.DataSource = doctors;//为GridView控件指名数据源
                gdvDoctor.DataBind();//GridView控件绑定数据
            }
            else
            {
                Response.Write("<script language='javascript'>alert('发生错误！医生表未查询到数据');</script>");
            }
        }
        protected void gdvDoctor_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdvDoctor.PageIndex = e.NewPageIndex;
            gdvDoctorBind();
        }

        protected void gdvDoctor_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gdvDoctor.EditIndex = -1;
            gdvDoctorBind();
        }

        protected void gdvDoctor_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //执行循环，保证每条数据都可以更新
            for (int i = 0; i <= gdvDoctor.Rows.Count; i++)
            {
                //首先判断是否是数据行
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //当鼠标停留时更改背景色
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#d3dfc0'");
                    //当鼠标移开时还原背景色
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
            }
        }

        protected void gdvDoctor_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string departID = gdvDoctor.DataKeys[e.RowIndex].Values["id"].ToString(); //取出要删除记录的主键值
            string msg = ManagerDoctor.deleteDoctor(Convert.ToInt32(departID));//执行删除并获取返回信息
            Response.Write("<script language=javascript>alert('" + msg + "');</script>");//弹出提示
            gdvDoctor.EditIndex = -1;
            gdvDoctorBind();//重新绑定数据
        }

        protected void gdvDoctor_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gdvDoctor.EditIndex = e.NewEditIndex;  //GridView编辑项索引等于单击行的索引
            gdvDoctorBind();
        }

        protected void gdvDoctor_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //封装修改对象
            int id = int.Parse(gdvDoctor.DataKeys[e.RowIndex].Values["id"].ToString());
            string doctorName = ((TextBox)gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorName")).Text.ToString();
            string nickName = ((TextBox)gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorNickName")).Text.ToString();
            string sex = ((TextBox)gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorSex")).Text.ToString();
            int age = int.Parse(((TextBox)(gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorAge"))).Text.ToString());
            string email = ((TextBox)gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorEmail")).Text.ToString();
            string telephone = ((TextBox)gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorTelephone")).Text.ToString();
            string address = ((TextBox)gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorAddress")).Text.ToString();
            string doctor_password = ((TextBox)gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorPassword")).Text.ToString();
            int department_id = int.Parse(((TextBox)(gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorDepartment_id"))).Text.ToString());
            string CREATED_BY = ((TextBox)(gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorCreater"))).Text.ToString();
            DateTime CREATED_TIME = DateTime.Parse(((TextBox)(gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorCreateTime"))).Text.ToString());
            string UPDATED_BY = ((TextBox)(gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorUpdater"))).Text.ToString();
            DateTime UPDATED_TIME = DateTime.Parse(((TextBox)(gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorUpdateTime"))).Text.ToString());
            int state = int.Parse(((TextBox)(gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorState"))).Text.ToString());

            Doctor doctor = new Doctor(
                id,doctorName,nickName,sex,age,email, telephone,address,doctor_password,department_id,
                CREATED_BY,
                CREATED_TIME,
                UPDATED_BY,
                UPDATED_TIME,
                state
                );
            string msg = ManagerDoctor.updateDoctor(doctor);//执行修改
            gdvDoctor.EditIndex = -1;
            gdvDoctorBind();//刷新显示
            Response.Write("<script language=javascript>alert('" + msg + "');</script>");//返回提示
        }

        protected void btnDoctorSubmit_Click(object sender, EventArgs e)
        {
            string msg = ManagerDoctor.insertDoctor(txtAddDoctor.Text);//执行插入
            gdvDoctor.EditIndex = -1;
            gdvDoctorBind();
            Response.Write("<script language=javascript>alert('" + msg + "');</script>");
        }


//下边是疾病管理部分-----------------------------------------------------------------------------------
        
        /// <summary>
        /// 绑定疾病管理GridView数据源
        /// </summary>
        private void gdvIllnessBind()
        {
            List<Illness> illnesses = ManagerIllness.getAllIllness();
            if (illnesses != null)
            {
                gdvIllness.DataSource = illnesses;
                gdvIllness.DataBind();
            }
        }
        /// <summary>
        /// 提交绑定疾病与科室之间的关系
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddIllAndDep_Click(object sender, EventArgs e)
        {
            string msg = "";
            int illId = int.Parse(tbxIllId.Text);
            int depId = int.Parse(tbxDepId.Text);
            if(illId>0&& depId > 0)
            {
                msg = ManagerIllness.addIllAndDep(illId, depId);
            }
            else
            {
                msg = "输入有误";
            }
            Response.Write("<script language=javascript>alert('" + msg + "');</script>");
        }
        /// <summary>
        /// 
        /// 执行查询疾病查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try{
                ILLID = int.Parse(txtSearchIllDep.Text);
            }
            catch (FormatException)
            {
                Response.Write("<script language=javascript>alert('输入有误');</script>");
            }
            gdvillAndDepBind();
        }
        /// <summary>
        /// 提交疾病的添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddIll_Click(object sender, EventArgs e)
        {
            string msg = ManagerIllness.insertIllness(txtIllAdd.Text);//执行插入
            gdvIllnessBind();
            Response.Write("<script language=javascript>alert('" + msg + "');</script>");
        }


        protected void gdvIllness_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdvIllness.PageIndex = e.NewPageIndex;
            gdvIllnessBind();
        }

        protected void gdvIllness_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gdvIllness.EditIndex = -1;
            gdvIllnessBind();
        }

        protected void gdvIllness_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //执行循环，保证每条数据都可以更新
            for (int i = 0; i <= gdvIllness.Rows.Count; i++)
            {
                //首先判断是否是数据行
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //当鼠标停留时更改背景色
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#d3dfc0'");
                    //当鼠标移开时还原背景色
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
            }
        }

        protected void gdvIllness_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string departID = gdvIllness.DataKeys[e.RowIndex].Values["id"].ToString(); //取出要删除记录的主键值
            string msg = ManagerIllness.deleteIllnessById(Convert.ToInt32(departID));//执行删除并获取返回信息
            Response.Write("<script language=javascript>alert('" + msg + "');</script>");//弹出提示
            gdvIllness.EditIndex = -1;
            gdvIllnessBind();//重新绑定数据
        }

        protected void gdvIllness_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gdvIllness.EditIndex = e.NewEditIndex;  //GridView编辑项索引等于单击行的索引
            gdvIllnessBind();
        }


        protected void gdvIllness_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //封装修改对象
            int id = int.Parse(gdvDoctor.DataKeys[e.RowIndex].Values["id"].ToString());
            string illness_name = ((TextBox)gdvDoctor.Rows[e.RowIndex].FindControl("illness_name")).Text.ToString();
            string CREATED_BY = ((TextBox)(gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorCreater"))).Text.ToString();
            DateTime CREATED_TIME = DateTime.Parse(((TextBox)(gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorCreateTime"))).Text.ToString());
            string UPDATED_BY = ((TextBox)(gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorUpdater"))).Text.ToString();
            DateTime UPDATED_TIME = DateTime.Parse(((TextBox)(gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorUpdateTime"))).Text.ToString());
            int state = int.Parse(((TextBox)(gdvDoctor.Rows[e.RowIndex].FindControl("txtDoctorState"))).Text.ToString());
            Illness illness = new Illness(id, illness_name, CREATED_BY, CREATED_TIME, UPDATED_BY, UPDATED_TIME, state);
            
            //执行修改
            string msg = ManagerIllness.updateIllness(illness);
            gdvIllness.EditIndex = -1;
            gdvIllnessBind();//刷新显示
            Response.Write("<script language=javascript>alert('" + msg + "');</script>");//返回提示
        }



        private static int ILLID = -1;//被查询的疾病id
        /// <summary>
        /// 绑定疾病与科室的GridView数据源
        /// </summary>
        private void gdvillAndDepBind()
        {
            List<Illness_departments> departments = null;
            if (ILLID != -1)
            {
                departments = ManagerIllness.findIllness_departmentsByIllsId(ILLID);
            }
            if (departments != null)
            {
                gdvillAndDep.DataSource = departments;//为GridView控件指名数据源
                gdvillAndDep.DataBind();//GridView控件绑定数据
            }
        }
        protected void gdvillAndDep_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdvillAndDep.PageIndex = e.NewPageIndex;
            gdvillAndDepBind();
        }

        protected void gdvillAndDep_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gdvillAndDep.EditIndex = -1;
            gdvillAndDepBind();
        }

        protected void gdvillAndDep_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //执行循环，保证每条数据都可以更新
            for (int i = 0; i <= gdvillAndDep.Rows.Count; i++)
            {
                //首先判断是否是数据行
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //当鼠标停留时更改背景色
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#d3dfc0'");
                    //当鼠标移开时还原背景色
                    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                }
            }
        }

        protected void gdvillAndDep_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string departID = gdvillAndDep.DataKeys[e.RowIndex].Values["id"].ToString(); //取出要删除记录的主键值
            string msg = ManagerIllness.deleteIllness_departments(Convert.ToInt32(departID));//执行删除并获取返回信息
            Response.Write("<script language=javascript>alert('" + msg + "');</script>");//弹出提示
            gdvDoctor.EditIndex = -1;
            gdvDoctorBind();//重新绑定数据
        }

        protected void gdvillAndDep_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gdvillAndDep.EditIndex = e.NewEditIndex;  //GridView编辑项索引等于单击行的索引
            gdvillAndDepBind();
        }

        protected void gdvillAndDep_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
        }

    }
}